package com.paobuji.aidebao.myapplication.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.paobuji.aidebao.myapplication.R;
import com.paobuji.aidebao.myapplication.bean.GetAllQuestion;

import java.util.ArrayList;

/**
 * Created by lenovo on 2017/3/16.
 */

public class MyHelpAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private LayoutInflater mInflater;
    private ArrayList activityList;
    Context context;


    private static OnItemClickListener mOnItemClickListener;
    //设置点击和长按事件

    public interface OnItemClickListener {
        void onItemClick(View view, int position,ArrayList arrayList); //设置点击事件
    }
    public static void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    /**
     * 构造器
     * @param context
     */
    public MyHelpAdapter(Context context, ArrayList activityList) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.activityList = activityList;
    }

    /**
     * item显示类型
     * 引入布局
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        //进行判断显示类型，来创建返回不同的View
        View view = LayoutInflater.from(context).inflate(R.layout.item_helplist, parent, false);
        //view.setBackgroundColor(Color.RED);
        ItemViewHolder itemViewHolder = new ItemViewHolder(view);
        return itemViewHolder;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        //绑定事件
        ((ItemViewHolder) holder).helpTitle.setText((position + 1) + ". " +((GetAllQuestion.DataBean.QuestionlistBean)activityList.get(position)).getQuesTitle());
        if (mOnItemClickListener != null) {
            ((ItemViewHolder) holder).helpTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(((ItemViewHolder) holder).helpTitle, position,activityList);
                }
            });
        }
    }

    /**
     * 要显示的item数目
     *
     * @return
     */
    @Override
    public int getItemCount() {

        //由于添加了footer所以返回的值都要加1个
        return activityList.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ItemViewHolder extends RecyclerView.ViewHolder {
        public TextView helpTitle;

        public ItemViewHolder(View view) {
            super(view);
            helpTitle = (TextView) view.findViewById(R.id.helpTitle);
        }
    }

    public void refreshItem(ArrayList newDatas) {
        //这里下拉刷新的数据是加到头部的
        ArrayList<String> a1 = new ArrayList<>();
        a1.addAll(newDatas);
        a1.addAll(activityList);
        activityList = a1;
        notifyDataSetChanged();
    }

    public void addMoreData(ArrayList moreList) {
        //上啦加载更多的数据是加载到末尾的
        activityList.addAll(moreList);
        notifyDataSetChanged();
    }

    //用网络获取的新数据替换缓存的数据
    public void changeData(ArrayList moreList) {
        //上啦加载更多的数据是加载到末尾的
        activityList = moreList;
        notifyDataSetChanged();
    }
}
